package 数组顺序结构;

public class ArrayList {
    Animals[] array;
    private int maxSize;
    private int length;

    ArrayList(int size){
        maxSize = size + 1;
        length = 0;
        array = new Animals[maxSize];
    }

    void addData(int index,Animals data){
        if(length == maxSize - 1 || length == maxSize){      //注意这个判定条件
            System.out.println("该线性表已满");
            return;
        }

        for (int i = index; i <= length; i++) {  //如果判定条件为 length == maxSize 那么在进行插入数据的操作时就会越界
            Animals temp = array[i];
            array[i] = data;
            data = temp;
        }
        length++;
    }

    int valueOfLength(){
        return length;
    }

    void deleteData(int index){
        for (int i = index; i < length; i++) {   //注意这个循环条件不是 i <= length ,而是 i < length
            array[i] = array[i + 1];
        }
        length--;
    }

    void clean(){
        length = 0;
        array = new Animals[length];
    }

    void orderInsert(Animals data){      // 这个代码有问题啊......................
        if(length == maxSize){
            System.out.println("数组已满");
            return;
        }
        array[length] = data;
        length++;
    }
}
